package com.znf4.controller.ad;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import com.znf4.ad.entity.Ad;
import com.znf4.ad.server.AdServer;
import com.znf4.constants.CommonConst;
import com.znf4.controller.common.BaseReturnModel;
import com.znf4.controller.util.JsonUtils;
import com.znf4.enums.OperationStatusEnum;
import com.znf4.id.IdGenerator;
import com.znf4.utils.OSSFile;

/**
 * 广告  http://123.206.66.232
 */
//@CrossOrigin(origins="http://127.0.0.1:8020")
@RestController
@RequestMapping("/merchant/ad")
public class AdController {
	
	private static final Logger LOG = LoggerFactory.getLogger(AdController.class);
	
	@Autowired
	private AdServer adServer ;
	
	@Autowired
	private IdGenerator idGenerator ;
	

	/**
	 * 函数功能说明 ： 添加广告
	 *
	 * @参数： @return
	 * @return String
	 * @throws
	 */
	@SuppressWarnings("rawtypes")
	@RequestMapping(value = "/add", method = { RequestMethod.POST, RequestMethod.GET })
	public void add(Ad ad , HttpServletRequest request , HttpServletResponse response) {
		
		try {
			ad.setId(idGenerator.getAdNameNumber());
			String fileName = CommonConst.AD_IMG_NUMBER + ad.getId();
			// 文件上传OSS
			Iterator iter = OSSFile.getFileIterator(request) ;
			if (iter != null ) {
				// 将request变成多部分request
				MultipartHttpServletRequest multiRequest = (MultipartHttpServletRequest) request;
				// 获取multiRequest 中所有的文件名
				while (iter.hasNext()) {
					// 一次遍历所有文件
					MultipartFile file = multiRequest.getFile(iter.next().toString());					
					ad.setImg(OSSFile.upFile(file , fileName));
					LOG.info("广告上传图片到oss.....并设置数据库参数。");
				}
			}
			adServer.addAd(ad);
		} catch (Exception e) {
			LOG.info(e.getMessage());
			JsonUtils.responseJson(response , BaseReturnModel.getMapModel("faild", "", OperationStatusEnum.ERROR.getDesc() ) );
//			return BaseReturnModel.getMapModel("faild", "", OperationStatusEnum.ERROR.getDesc() );
		}
//		return BaseReturnModel.getMapModel("success", "操作完成", "");
		JsonUtils.responseJson(response ,BaseReturnModel.getMapModel("success", "操作完成", "") );
	}

	
	
	/**
	 * 函数功能说明 ： 修改农作物商品
	 *
	 * @参数： @return
	 * @return String
	 * @throws
	 */
	@SuppressWarnings("rawtypes")
	@RequestMapping(value = "/update", method = { RequestMethod.POST, RequestMethod.GET })
	public Map<String, Object> update(Ad ad , HttpServletRequest request) {

		try {
			String fileName = CommonConst.AD_IMG_NUMBER + ad.getId();
			// 文件上传OSS
			Iterator iter = OSSFile.getFileIterator(request);
			if (iter != null) {
				// 将request变成多部分request
				MultipartHttpServletRequest multiRequest = (MultipartHttpServletRequest) request;
				// 获取multiRequest 中所有的文件名
				while (iter.hasNext()) {
					// 一次遍历所有文件
					MultipartFile file = multiRequest.getFile(iter.next().toString());
					ad.setImg(OSSFile.upFile(file, fileName));
				}
			}
			adServer.updateAd(ad);
		} catch (Exception e) {
			LOG.info("修改农作物商品信息 :", e.getMessage());
			return BaseReturnModel.getMapModel("faild", "",  OperationStatusEnum.ERROR.getDesc()) ;
		}

		return BaseReturnModel.getMapModel("success", "操作完成", "") ;
	}
	
	

	/**
	 * 函数功能说明 ： 删除广告
	 *
	 * @参数： @return
	 * @return String
	 * @throws
	 */
	@RequestMapping(value = "/delOne", method = { RequestMethod.POST, RequestMethod.GET })
	public Map<String, Object> delOne(Ad ad ) {
		
		try {
			adServer.delOneAd(ad);
			// 删除文件 OSS
			OSSFile.deleteFile(ad.getImg());
		} catch (Exception e) {
			LOG.info("删除单个广告信息 :", e.getMessage());
			e.printStackTrace();
			return BaseReturnModel.getMapModel("faild", "",  OperationStatusEnum.ERROR.getDesc()) ;
		}
		return BaseReturnModel.getMapModel("success", "操作完成", "") ;
	}
	
	
	/**
	 * 函数功能说明 ： 删除多条农作物商品
	 *
	 * @参数： @return
	 * @return String
	 * @throws
	 */
	@RequestMapping(value = "/delAll", method = { RequestMethod.POST, RequestMethod.GET })
	public Map<String, Object> delAll(String[] ads, Model model) {
		try {
			List<Ad> lAd = new ArrayList<>();
			for (int i = 0; i < ads.length; i++) {

				Ad ad = new Ad();
				ad.setId(ads[i]);
				lAd.add(ad);

				// 删除文件
				Ad delAd = adServer.queryOne(ad);
				OSSFile.deleteFile(delAd.getImg());

			}
			adServer.delAllAd(lAd);
			model.addAttribute("status", OperationStatusEnum.SUCCESS.getDesc());
		} catch (Exception e) {
			LOG.info("删除多个其他商品信息:", e.getMessage());
			return BaseReturnModel.getMapModel("faild", "",  OperationStatusEnum.ERROR.getDesc()) ;
		}
		return BaseReturnModel.getMapModel("success", "操作完成", "") ;
	}
	
	/**
	 * 函数功能说明 ： 获取所有广告
	 *
	 * @参数： @return
	 * @return String
	 * @throws
	 */
	@RequestMapping(value = "/queryAll", method = { RequestMethod.POST, RequestMethod.GET })
	public Map<String, Object> queryAll(Ad ad  , Model model , HttpServletRequest request) {
		
		System.out.println(request.getRemoteAddr());
		System.out.println(request.getRemotePort());
		List<Ad> list = adServer.queryAll(ad);
		return BaseReturnModel.getMapModel("success", list , "") ;
	}

	

}
